package com.lt.experiment03.mission03;
import java.sql.*;

/**
 * @author LT
 * @version 1.0
 * @PackageName com.lt.experiment03.mission03
 * @ClassName DBHelper
 * @Description to do
 * @date 2021/10/17 19:21
 */
public final class DBHelperSingleton {
    private String url = "jdbc:mysql://localhost:3306/lttest";
    private String user = "root";
    private String password = "123456";

    private static DBHelperSingleton instance = null;

    private DBHelperSingleton() {
    }

    public static DBHelperSingleton getInstance() {
        if (instance == null) {
            synchronized (DBHelperSingleton.class) {
                if (instance == null) {
                    instance = new DBHelperSingleton();
                }
            }
        }
        return instance;
    }

    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url, user, password);
    }

    public void free(ResultSet rs, Statement st, Connection conn) {
        try {
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (st != null) {
                    st.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    public PreparedStatement getps(Connection conn,String sql){
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ps;
    }

}
